package org.finesys.chat.core.base.embedding;

import org.finesys.chat.core.base.embedding.filter.Filter;
import org.finesys.common.core.util.ValidationUtil;

import java.util.Objects;

public class EmbeddingSearchRequest {

    private final Embedding queryEmbedding;
    private final int limit;
    private final double minScore;
    private final Filter filter;
    private final boolean isAsc;

    public EmbeddingSearchRequest(Embedding queryEmbedding, int limit, double minScore, Filter filter, boolean isAsc) {
        this.queryEmbedding = ValidationUtil.ensureNotNull(queryEmbedding, "queryEmbedding");
        this.limit = ValidationUtil.ensureGreaterThanZero(limit, "limit");
        this.minScore = ValidationUtil.ensureBetween(minScore, 0.0, 1.0, "minScore");
        this.filter = filter;
        this.isAsc = isAsc;
    }

    public Embedding getQueryEmbedding() {
        return queryEmbedding;
    }

    public int getLimit() {
        return limit;
    }

    public double getMinScore() {
        return minScore;
    }

    public Filter getFilter() {
        return filter;
    }

    public boolean isAsc() {
        return isAsc;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) return true;
        if (object == null || getClass() != object.getClass()) return false;
        EmbeddingSearchRequest that = (EmbeddingSearchRequest) object;
        return limit == that.limit && Double.compare(minScore, that.minScore) == 0 && isAsc == that.isAsc && Objects.equals(queryEmbedding, that.queryEmbedding) && Objects.equals(filter, that.filter);
    }

    @Override
    public int hashCode() {
        return Objects.hash(queryEmbedding, limit, minScore, filter, isAsc);
    }

    @Override
    public String toString() {
        return "EmbeddingSearchRequest{" +
                "queryEmbedding=" + queryEmbedding +
                ", limit=" + limit +
                ", minScore=" + minScore +
                ", filter=" + filter +
                ", isAsc=" + isAsc +
                '}';
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private Embedding queryEmbedding;
        private int limit = 10;
        private double minScore = 0.0;
        private Filter filter;
        private boolean isAsc = false;

        Builder() {
        }

        Builder(EmbeddingSearchRequest request) {
            this.queryEmbedding = request.getQueryEmbedding();
            this.limit = request.getLimit();
            this.minScore = request.getMinScore();
            this.filter = request.getFilter();
        }

        public Builder queryEmbedding(Embedding val) {
            this.queryEmbedding = val;
            return this;
        }

        public Builder limit(int val) {
            this.limit = val;
            return this;
        }

        public Builder minScore(double val) {
            this.minScore = val;
            return this;
        }

        public Builder filter(Filter val) {
            this.filter = val;
            return this;
        }

        public Builder isAsc(boolean val) {
            this.isAsc = val;
            return this;
        }

        public EmbeddingSearchRequest build() {
            return new EmbeddingSearchRequest(queryEmbedding, limit, minScore, filter, isAsc);
        }
    }
}
